﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false" CodeFile="recRestructurlist.aspx.cs" Inherits="SIPO.Web.Lists.Transaction.recRestructurlist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <div>
        <button id="btnAdd" type="button" onclick="OpenForm('add');">
            Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">
            Ubah</button>
            <%--  <button id="btnDelete" type="button" onclick="OpenForm('delete');">
            Hapus</button>--%>
    </div>
    <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog" title="Default dialog">
    </div>
    <script type="text/javascript">
        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
    </script>
    <script type="text/javascript">

        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurhandler.ashx") %>';
        var handlerSubUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurDetailhandler.ashx") %>';

        $('#table').jqGrid({
            url: handlerUrl + '?method=getdata',
            datatype: 'json',
            colNames: ['ID', 'ID Debitur', 'Nama Debitur', 'No. Surat', 'Tgl. Surat', 'Keputusan',
                'Nilai Tagihan', 'Nilai Keputusan (Rp)', 'File SK','Lampiran', 'Jadwal Pembayaran'],
            colModel: [
                    { name: 'RestructurID', hidden: true, index: 'RestructurID', width: 60 },
                    { name: 'DebtorID', index: 'DebtorID', width: 75 },
                    { name: 'DebtorName', index: 'DebtorName', align: 'center', width: 0 },
                    { name: 'LetterNo', index: 'LetterNo', align: 'center', width: 0 },
                    { name: 'LetterDate', index: 'LetterDate', align: 'center', width: 0,
                        formatter: 'date', formatoptions: { newformat: 'd M Y' }
                    },
                    { name: 'ActionRestructurName', index: 'ActionRestructurName', align: 'center', width: 0 },
                    { name: 'SumValues', index: 'SumValues', formatter: 'number',
                        align: 'right', width: 0
                    },
                    { name: 'RestructurValues', index: 'RestructurValues', formatter: 'number',
                        align: 'right', width: 0
                    },
                    { name: 'RestructurID', align: 'center', formatter: linkDownloadFile, width: 85 },
                    { name: 'AttachFileName', index: 'AttachFileName', align: 'center', width: 125 },
                    { name: 'RestructurID', align: 'center', formatter: linkDetail, width: 150 }

           	],
            rowNum: 10,
            id: 'RestructurID',
            rownumbers: true,
           multiselect: true,
            rowList: [5, 10, 15, 20],
            pager: '#pager',
            sortname: 'DebtorName',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 1250,
            height: '100%',
            caption: 'Restrukturisasi',
            ajaxGridOptions: { cache: false },
            ondblClickRow: function () {
                OpenForm('edit');
            },
            subGrid: true,
            subGridRowExpanded: function (subgridId, rowId) {
                var subgridTableId = subgridId + "_t";
                $("#" + subgridId).html("<table id='" + subgridTableId + "'></table>");
                $("#" + subgridTableId).jqGrid({
                    datatype: 'json',
                    url: handlerSubUrl + '?method=getdatabyresid&rowid=' + rowId,
                    colNames: ['ID Transaksi','Jenis Pungutan', 'Nilai'],
                    colModel: [
                      { name: "IDTransaksi", width: 130 },
                     // { name: "NoSanksi", width: 130 },
                      { name: "JenisPungutan", width: 130 },
                      { name: "Nilai", width: 130,align: 'right', formatter: 'currency', 
                        formatoptions: { decimalSeparator: ",", thousandsSeparator: ".", decimalPlaces: 2 }}
                    ],
                    height: "100%",
                    rowNum: 10,
                    rownumbers: true,
                    idPrefix: "s_" + rowId + "_"
                });
            }
        });

        $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, search:false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true });

        function linkDownloadFile(cellValue, options, rowdata) {
            var RestID = encodeURIComponent(cellValue);
            return "<a  href='#' onclick='downloadfile(" + cellValue + ")' >download</a>";
        }
        function linkDetail(cellValue, options, rowdata) {
            var RestID = encodeURIComponent(cellValue);
            return "<a href=\"" + "#" + "\"" + " onclick=\"" + "popupform('" + RestID + "')" + "\"" + ">detail</a>";
        }

        function popupform(cellValue) {
            var formUrl = '<%= ResolveUrl("~/forms/Transaction/recRestructurNextform.aspx") %>';
            $('#dialog').load(formUrl + '?id=' + cellValue + '&status=view&menuid=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 825, title: 'Jadwal Pembayaran' });
            $("#dialog").dialog('open');
        }

        function downloadfile(cellValue) {
            var objrecSanction = new Object();
            objrecSanction.ID = encodeURIComponent(cellValue);
            jsonData = JSON.stringify(objrecSanction);
            var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx") %>';
            $.ajax({
                type: 'POST',
                url: handlerUrl + "?method=checkfilerestructure",
                contentType: 'application/json',
                dataType: 'json',
                data: jsonData,
                success: function (data) {
                    if (data.IsSuccess) {
                        window.open(handlerUrl + '?method=downloadrestructure&id=' + cellValue, 'popUpWindow', 'height=400,width=800,left=300,top=200,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no, status=yes');
                        //return "<a target='_blank' href='" + handlerUrl + "method=download&sanction_id=" + cellValue + "' >download</a>";
                    }
                    else {
                        alert(data.Message);
                    }
                },
                error: function (response) {
                    //var r = JSON.parse(response.responseText);
                    alert("Error Check File Download"); //Message: " + r.Message);
                }
            });

        }


        function OpenForm(ctl) {
            var formUrl = '<%= ResolveUrl("~/forms/Transaction/recRestructurform.aspx") %>';

            var grid = $('#table');
            var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
            $.each(selIds, function () {
                selText.push(grid.jqGrid('getCell', this, 'RestructurID'));
            });
            if (ctl == 'edit') {
                if (selIds.length == 0)
                    alert('Pilih data yang mau di edit!');
                else {
                    if (selIds.length > 1) {
                        alert('Pilih satu record untuk ubah data!');
                    }
                    else if (selIds.length == 1) {
                        $('#dialog').load(formUrl + '?id=' + selText).dialog({ autoOpen: false, modal: true, width: 900, title: 'Ubah Restrukturisasi' });
                        $("#dialog").dialog('open');
                    }
                }
            } else if (ctl == 'add') {
                $('#dialog').load(formUrl).dialog({ autoOpen: false, modal: true, width: 900, title: 'Input Restrukturisasi' });
                $("#dialog").dialog('open');
            }
            else {
                if (selIds.length == 0)
                    alert('Pilih data yang mau di hapus!');
                else {
                    var result = window.confirm('Benar data akan di hapus ?');
                    if (result) {
//                        var objrecSanction = new Object();
//                        objrecSanction.SanctionID = encodeURIComponent(selText);
//                        jsonData = JSON.stringify(objrecSanction);
//                        CallAjax('<%= ResolveUrl("~/ajaxhandler/transaction/recSanctionhandler.ashx") %>', "?method=delete", jsonData);
//                        setTimeout("$('#table').trigger('reloadGrid');", 3000);
                    }
                }
            }
        }
        </script>
</asp:Content>

